package com.two.screen.water.balance.print.bean

import com.two.screen.water.balance.bean.OrderServerBean
import com.two.screen.water.balance.bean.database.OrderDB
import com.two.screen.water.balance.net.api.DataApi
import com.two.screen.water.balance.net.api.OrderApi.ORDER_STATUS_PAID


class PrintOrderBean {
    var title: String? = null

    //var marketName: String? = null//市场名称
    var collectionTime: String? = null//收银时间
    var operationName: String? = null//操作人员
    var orderSn: String? = null//订单编号
    var orderPriceInfo: String? = null//订单金额
    var payInfo: String? = null//支付信息
    var qrCode: String? = null//二维码
    var qrCodeTipsTop: String? = null//二维码 提示语
    var qrCodeTipsBottom: String? = null//二维码 提示语
    var orderTotal: String? = null//商品总数量和金额
    var orderState: String? = null//支付状态
    var productList: MutableList<PrintProductBean>? = null//商品信息

    companion object {
        fun orderDB2PrintOrderBean(orderDB: OrderDB?): PrintOrderBean {
            return PrintOrderBean().apply {
                var totalPrice = 0.0 //商品总额
                var totalGift = 0.0 //商品总优惠
                var totalWeight = 0.0
                this.productList = mutableListOf()
                val orderProductDBList = try {
                    orderDB?.orderProductDBList
                } catch (e: Exception) {
                    null
                }
                orderProductDBList?.forEach {
                    totalPrice += try {
                        (it.realPrice.toDouble() * it.weight.toDouble())
                    } catch (e: Exception) {
                        0.0
                    }
                    totalGift += try {
                        it.giftPrice.toDouble()
                    } catch (e: Exception) {
                        0.0
                    }

                    totalWeight += it.weight.toDouble()
                    this.productList?.add(PrintProductBean().apply {
                        var price = it.discountPercent / 100.0 * (it.realPrice?.toDouble() ?: 0.0)
                        //var subPrice = price * (it.weight?.toDouble() ?: 0.0)
                        this.name = it.productName
                        this.price = String.format("%.2f", price).plus("元")
                        this.weight = it.weight.plus(it.unitName)
                        this.subPrice = String.format("%.2f", it.subTotalPrice?.toFloat() ?: 0.0).plus("元")
                    })
                }
                this.orderState = when (orderDB?.orderStatus) {//0未支付;1支付中;2已支付;3退货;4:反结账;5：取消支付；
                    ORDER_STATUS_PAID -> "已支付"
                    else -> "未支付"
                }

                this.title = "订单信息"
                this.orderSn = orderDB?.orderSn
                this.collectionTime = orderDB?.payTimeName
                this.operationName = orderDB?.userName
                var discountPercent = if (orderDB?.discountPercent == 100) "" else "(${String.format("%.1f", (orderDB?.discountPercent ?: 100) / 10.0)}折)"


                this.orderPriceInfo =
                    "商品总额：￥${String.format("%.2f", totalPrice)}\n商品优惠：￥${String.format("%.2f", totalGift)}\n" +
                            "订单总金额：￥${orderDB?.totalPrice ?: "0.00"}\n整单优惠${discountPercent}：￥${orderDB?.giftPrice ?: "0.00"}"

                this.orderTotal = "${String.format("%.3f", totalWeight)},总价:${orderDB?.totalPrice ?: "0.00"}元"

                var findZero = try {
                    (orderDB?.findZero ?: "0.0").toDouble()
                } catch (e: Exception) {
                    0.0
                }
                var fz = if (findZero == 0.0) "" else ", 找零￥${String.format("%.2f", findZero)}"
                this.payInfo = "${orderDB?.payTypeName ?: ' '}￥${orderDB?.realPrice ?: 0}${fz}\n支付状态：${this.orderState}"

                this.qrCodeTipsTop = DataApi.mPrintQrTopTips
                this.qrCodeTipsBottom = DataApi.mPrintQrBottomTips
                this.qrCode = orderDB?.integralUrl//TODO:这里应该还需要配置个离线订单二维码。

            }
        }

        fun orderServerBean2PrintOrderBean(find: OrderServerBean): PrintOrderBean? {
            return PrintOrderBean().apply {
                this.productList = mutableListOf()
                var totalWeight = 0.0
                find.detail?.forEach {
                    this.productList?.add(PrintProductBean().apply {
                        var price = it.discount * (it.price?.toDouble() ?: 0.0)
                        this.name = it.name
                        this.price = String.format("%.2f", price).plus("元")
                        this.weight = (if (it.unit == 1) it.weight else if (it.unit == 2) it.number else "").plus(it.unit_name)
                        this.subPrice = it.subtotal
                    })
                    totalWeight += when (it.unit) {
                        1 -> it.weight?.toDouble() ?: 0.0
                        2 -> it.number?.toDouble() ?: 0.0
                        else -> 0.0
                    }
                }
                this.orderState = when (find.status_member) {
                    0 -> "未支付"
                    5 -> "已支付"
                    else -> "未知"
                }
                this.title = "订单信息"
                this.orderSn = find.order_sn
                this.collectionTime = find.pay_date
                this.operationName = find.operation_name

                var discountPercent = if (find.discount == 1.0) "" else "(${String.format("%.1f", find.discount * 10.0)}折)"
                this.orderPriceInfo =
                    "商品总额：￥${find.all_price}\n商品优惠：￥${find.goods_discount_amount}\n" +
                            "订单总金额：￥${find.order_price}\n整单优惠${discountPercent}：￥${find.discount_amount}"

                this.orderTotal = "${String.format("%.3f", totalWeight)},总价:${find.order_price ?: "0.00"}元"

                var fz = if (find.diff_price == 0.0) "" else ", 找零￥${find.diff_price}"
                this.payInfo = "${find.pay_type_title ?: ' '}￥${find.total_price ?: 0}${fz}\n支付状态：${this.orderState}"
                this.qrCodeTipsTop = DataApi.mPrintQrTopTips
                this.qrCodeTipsBottom = DataApi.mPrintQrBottomTips
                this.qrCode = find.cy_url //
            }
        }

    }
}

class PrintProductBean() {
    var name: String? = null
    var price: String? = null
    var weight: String? = null
    var subPrice: String? = null
}


